/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PantallaPrincipal.java
 *
 * Created on 8/10/2013, 11:16:55 AM
 */

package checadorhuellav2.interfaz;

import com.griaule.grfingerjava.GrFingerJavaException;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import checadorhuellav2.procedimientos.Util;
import checadorhuellav2.si_conexion;
import checadorhuellav2.si_variables;

/**
 *
 * @author michelle
 */
public class PantallaPrincipal extends javax.swing.JFrame {

    private Util procedimientosSDK;
//Panel para mostrar la huella digital
private JPanel fingerprintViewPanel = null;
//Imagen de la huella actual
private BufferedImage fingerprintImage = null;
 si_conexion x = new si_conexion();

    /** Creates new form PantallaPrincipal */
    public PantallaPrincipal() {
        inicializar();
        initComponents();

        procedimientosSDK.inicializarCaptura();
        setLocationRelativeTo(null);
        setVisible(true);
    }

public void inicializar() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.err.println("No se pudo aplicar el estilo visual");
}
//Crea una instancia de Util
this.procedimientosSDK = new Util(this);
}
  /**
* @function: crearPanelHuella
* @author: Ricardo Rosero
* @access: private
* @return
*/
private JComponent crearPanelHuella()
    {
        //Crea un panel nuevo para mostrar la huella
    System.err.println("creando grafico cruz");
        fingerprintViewPanel = new JPanel()
            {
                //Se sobreescribe el método paintComponent para habilitar la muestra de la imagen de la huella
                
            @Override
                public void paintComponent(Graphics g)
                    {
                        super.paintComponent(g);
                        //Si hay una imagen para ser mostrada
                        
                        if (fingerprintImage!=null)
                            {
                                //Calcula el tamaño y posición de la imagen para ser pintada el tamaño es ajustado para que ocupe todo el tamaño del panel

                                Insets insets = getInsets();
                                int transX = insets.left;
                                int transY = insets.top;
                                int width=getWidth()-getInsets().right-getInsets().left;
                                int height = getHeight() - getInsets().bottom - getInsets().top;
                                //Se dibuja la imagen
                                g.drawImage(fingerprintImage, transX, transY, width, height, null);
                                System.err.println("creando grafico");
                            }
                    }
            };
    //Se agrega un border alrededor del panel
    fingerprintViewPanel.setBorder(new CompoundBorder (
    new EmptyBorder (2,2,2,2),
    new BevelBorder(BevelBorder.LOWERED)));
    fingerprintViewPanel.setToolTipText("Imagen de la última huella capturada.");
    if(fingerprintViewPanel==null)
        {
            System.exit(1);
            return null;
        }
    else
        {
            return fingerprintViewPanel;
        }
}
/**
* @function: showImage
* @author: Ricardo Rosero
* @access: public
* @return
*/
public void showImage(BufferedImage image)
    {
        //Utiliza el imageProducer para crear una imagen de la huella digital
        fingerprintImage = image;
        //Se dibuja la nueva imagen
        System.err.println("creando huella repaint");
        this.repaint();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelContenedor = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        btnVerificar = new javax.swing.JButton();
        btnIdentificar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        panelContenedor.setBorder(javax.swing.BorderFactory.createTitledBorder("Huella digital"));
        panelContenedor.setLayout(new java.awt.BorderLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Operaciones"));

        btnVerificar.setText("Identificar");
        btnVerificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerificarActionPerformed(evt);
            }
        });

        btnIdentificar.setText("Guardar");
        btnIdentificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIdentificarActionPerformed(evt);
            }
        });

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnVerificar)
                        .addGap(131, 131, 131)
                        .addComponent(btnIdentificar))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(btnSalir)))
                .addContainerGap(186, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnVerificar)
                    .addComponent(btnIdentificar))
                .addGap(36, 36, 36)
                .addComponent(btnSalir)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
    System.out.println("form windows activated:");
    this.panelContenedor.add(crearPanelHuella());

    }//GEN-LAST:event_formWindowActivated

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        x.cierraConexionv2(si_variables.getconexiongral());
        System.exit(0);// para cerrar la aplicacion


    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnIdentificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIdentificarActionPerformed
        try {
            // TODO add your handling code here:
            procedimientosSDK.guardar(si_variables.getempleado());
        } catch (GrFingerJavaException ex) {
            Logger.getLogger(PantallaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnIdentificarActionPerformed

    private void btnVerificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerificarActionPerformed
        try {
            // TODO add your handling code here:
            procedimientosSDK.identificarPersona();
        } catch (GrFingerJavaException ex) {
            Logger.getLogger(PantallaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnVerificarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PantallaPrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnIdentificar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnVerificar;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel panelContenedor;
    // End of variables declaration//GEN-END:variables

}
